package com.crane.oss.sys.service.impl;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crane.common.core.dto.SessUser;
import com.crane.common.core.exception.AppException;
import com.crane.common.core.exception.BasicErrorCode;
import com.crane.common.core.utils.StringUtil;
import com.crane.common.framework.utils.BeanUtil;
import com.crane.oss.sys.bean.request.menu.AddApiRequest;
import com.crane.oss.sys.bean.request.menu.QueryApiRequest;
import com.crane.oss.sys.bean.request.menu.UpdateApiRequest;
import com.crane.oss.sys.bean.resp.ApiBean;
import com.crane.oss.sys.dao.ApiDao;
import com.crane.oss.sys.model.Api;
import com.crane.oss.sys.service.ApiService;
import com.crane.oss.sys.service.MenuApiService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Service("apiService")
public class ApiServiceImpl implements ApiService {

    private static final Logger logger = LoggerFactory.getLogger(ApiServiceImpl.class);

    @Resource
    protected ApiDao apiDao;

    @Resource
    protected MenuApiService menuApiService;

    @Override
    public ApiBean queryApi(Long apiId) {
        logger.info("ApiServiceImpl queryViewBean apiId={}", apiId);
        Api api = apiDao.selectById(apiId);
        if (null == api) {
            throw new AppException(BasicErrorCode.ENTITY_NOT_EXISTS, "api");
        }
        ApiBean apiBean = new ApiBean();
        BeanUtil.copyProperties(api, apiBean);
        return apiBean;
    }

    @Override
    public IPage<ApiBean> queryList(QueryApiRequest queryRequest) {
        logger.info("ApiServiceImpl queryList queryRequest={}", JSON.toJSON(queryRequest));
        LambdaQueryWrapper<Api> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.likeLeft(StringUtil.isNotEmpty(queryRequest.getName()), Api::getName, queryRequest.getName());
        queryWrapper.eq(ObjectUtils.isNotNull(queryRequest.getStatus()), Api::getStatus, queryRequest.getStatus());
        queryWrapper.eq(Api::getType, queryRequest.getType());
        IPage<Api> page = apiDao.selectPage(new Page<>(queryRequest.getPageNo(), queryRequest.getPageSize()), queryWrapper);

        return page.convert(u -> {
            ApiBean v = new ApiBean();
            BeanUtils.copyProperties(u, v);
            return v;
        });
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(AddApiRequest addRequest, SessUser opUser) {
        logger.info("ApiServiceImpl save request={}", JSON.toJSON(addRequest));
        Api api = new Api();
        BeanUtil.copyProperties(addRequest, api);
        api.setCreateTime(new Date());
        api.setCreateUserId(opUser.getUserId());
        return apiDao.insert(api);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(UpdateApiRequest updateRequest, SessUser opUser) {
        logger.info("ApiServiceImpl update request={}", JSON.toJSON(updateRequest));
        Api api = apiDao.selectById(updateRequest.getId());
        BeanUtil.copyProperties(updateRequest, api);
        api.setUpdateTime(new Date());
        api.setUpdateUserId(opUser.getUserId());
        return apiDao.updateById(api);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(Long apiId) {
        logger.info("ApiServiceImpl delete apiId={}", apiId);
        apiDao.deleteById(apiId);
        return menuApiService.deleteByApiId(apiId);
    }

    @Override
    public List<ApiBean> queryApiListByMenuId(Long menuId) {
        logger.info("ApiServiceImpl queryqueryApiListByMenuId menuId={}", menuId);
        List<Api> apiList = apiDao.queryApiListByMenuId(menuId);
        List<ApiBean> apiBeanList = BeanUtil.copyList(apiList, ApiBean.class);
        return apiBeanList;
    }

    @Override
    public List<ApiBean> queryApiListByUser(Long userId) {
        logger.info("ApiServiceImpl queryqueryApiListByMenuId userId={}", userId);
        List<Api> apiList = apiDao.queryApiListByUserId(userId);
        List<ApiBean> apiBeanList = BeanUtil.copyList(apiList, ApiBean.class);
        return apiBeanList;
    }
}
